<template>
  <div class="register container">
    <Header>
        <slot>
          <span>重置密码</span>
        </slot>
    </Header>
    <section>
      <form action="">
        <div class="login-tel">
          <input type="tel" v-model="userPwd" placeholder="请输入新密码" />
        </div>
        <div class="login-btn" @click="submitBtn">确 认</div>
      </form>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Header from "@/views/login/Header.vue";
import Tabbar from "@/components/common/Tabbar.vue";
import { Toast } from 'mint-ui';
import http from '@/common/api/request';
export default {
  data () {
    return {
      userPwd:'',
      //验证规则
      rules: {
        userPwd: {
          rule: /^\w{6,12}$/,
          msg: "密码不能为空,并且要求6-12位",
        },
      },
    }
  },
  components: {
    Header,
    Tabbar,
  },
  methods:{
    goUserLogin() {
        this.$router.replace('/userLogin');
    },
        //验证信息提示
    validate(key) {
        let bool = true;
        if (!this.rules[key].rule.test(this[key])){
            Toast(this.rules[key].msg);
            bool = false;
            return false
        }
        return bool
    },
    //点击重置
    submitBtn() {
        //验证密码
        if (!this.validate("userPwd")){
            Toast("密码修改失败");
            return;
        }

        //如果正确，进行修改
        http.$axios({
        url:'/api/recovery',
        method:'POST',
        data:{
            phone:this.$route.query.phone,
            pwd:this.userPwd,
        }
        }).then(res=>{
        if (!res.success) return;
        Toast("修改成功");
            setTimeout(() => {
                this.$router.replace({
                path:'/login'
            })
            }, 1000);
        }) 
    }
  }
};
</script>

<style lang="scss" scoped>
header {
  span {
    font-size: .48rem;
  }
}
section {
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: .4rem;
    background-color: #f5f5f5;
    div {
        margin: .2667rem 0;
        width: 8.9333rem;
        height: 1.1733rem;
    }
    input {
        box-sizing: border-box;
        padding: 0 .2667rem;
        line-height: 1.1733rem;
        background-color: #fff;
        border: 1px solid #ccc;
        border-radius: .16rem;
    }
    .login-tel {
        margin-top: .8rem;
        input {
            width: 100%;
        }
    }
    .login-code {
        display: flex;
        input {
            flex: 1;
        }
        button {
            padding: 0 .5333rem;
            line-height: 1.1733rem;
            color: #fff;
            background-color: #b0352f;
            border: none;
            border-radius: .16rem;
        }
    }
    .login-btn{
        line-height: 1.1733rem;
        text-align: center;
        color: #fff;
        background-color: #b0352f;
        border-radius: .16rem;
    }
}
</style>